﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YunShop_WebAPI.BLL
{
    /// <summary>
    /// 参数设置
    /// </summary>
    public class SqlParameterList
    {
        List<SqlParameter> list_Para = new List<SqlParameter>();
        public SqlParameterList()
        {
            list_Para.Clear();
        }

        public SqlParameter GetParam(string name)
        {
            for (int i = 0; i < list_Para.Count; i++)
            {
                if (list_Para[i].ParameterName.Equals(name))
                    return list_Para[i];
            }
            return null;
        }
        /// <summary>
        /// 添加參數
        /// </summary>
        /// <param name="Parameter">參數定義</param>
        /// <param name="value">參數值</param>
        public void Add(SqlParameter Parameter, object value)
        {
            list_Para.Add(Parameter);
            list_Para.Last<SqlParameter>().Value = value;
        }

        /// <summary>
        /// 添加參數
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="dbType"></param>
        /// <param name="size"></param>
        /// <param name="value"></param>
        public void Add(string parameterName, SqlDbType dbType, int size, object value)
        {
            list_Para.Add(new SqlParameter(parameterName, dbType, size));
            list_Para.Last<SqlParameter>().Value = value;
        }

        /// <summary>
        /// 添加參數
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="dbType"></param>
        /// <param name="value"></param>
        public void Add(string parameterName, SqlDbType dbType, object value)
        {
            list_Para.Add(new SqlParameter(parameterName, dbType));
            list_Para.Last<SqlParameter>().Value = value;
        }

        /// <summary>
        /// 返回參數數組
        /// </summary>
        /// <returns></returns>
        public SqlParameter[] Get_List()
        {
            return list_Para.ToArray();
        }
    }

    public class VisualizationService
    {
        /// <summary>
        /// 门店拿货显示
        /// </summary>
        public static ArrayList GetDataByRequire(string mSystemAppID, int ipageIndex, int ipageSize)
        {
            int mUserID = SystemInfo.GetSystemAppID(mSystemAppID, SystemAppID.UserID);
            int mCorpID = SystemInfo.GetSystemAppID(mSystemAppID, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(mSystemAppID);
            try
            {
                string sql = @"exec pr_Vis_GetDataByRequire @mPageSize,@mPageIndex,@mCorpID";
                SqlParameterList par_master = new SqlParameterList();
                par_master.Add("@mCorpID", SqlDbType.Int, mCorpID);//用户ID
                par_master.Add("@mPageSize", SqlDbType.Int, 4, ipageSize);
                par_master.Add("@mPageIndex", SqlDbType.Int, 4, ipageIndex);
                ArrayList datalist = DBHelper.Select(sql, par_master.Get_List(), connstr);
                return datalist;
            }
            catch (Exception E)
            {
                return null;
            }
        }

        /// <summary>
        /// 门店营业排名
        /// </summary>
        public static ArrayList GetDataByStock(string shop_appid, int ipageIndex, int ipageSize)
        {
            int mUserID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID);
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            try
            {
                string sql = @"exec pr_Vis_GetDataByStock @mPageSize,@mPageIndex,@mCorpID";
                SqlParameterList par_master = new SqlParameterList();
                par_master.Add("@mCorpID", SqlDbType.Int, mCorpID);//用户ID
                par_master.Add("@mPageSize", SqlDbType.Int, 4, ipageSize);
                par_master.Add("@mPageIndex", SqlDbType.Int, 4, ipageIndex);
                ArrayList datalist = DBHelper.Select(sql, par_master.Get_List(), connstr);
                return datalist;
            }
            catch (Exception E)
            {
                return null;
            }
        }

        /// <summary>
        /// 6个月营业情况
        /// </summary>
        public static ArrayList GetDataByMonth(string shop_appid)
        {
            int mUserID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID);
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            try
            {
                string sql = @"exec pr_Vis_GetDataByMonth @mCorpID";
                SqlParameterList par_master = new SqlParameterList();
                par_master.Add("@mCorpID", SqlDbType.Int, mCorpID);//用户ID
                ArrayList datalist = DBHelper.Select(sql, par_master.Get_List(), connstr);
                return datalist;
            }
            catch (Exception E)
            {
                return null;
            }
        }

        /// <summary>
        /// 门店销售分析
        /// </summary>
        public static ArrayList GetStockAnalysis(string shop_appid)
        {
            int mUserID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID);
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            try
            {
                string sql = @"exec pr_Vis_GetStockAnalysis @mCorpID";
                SqlParameterList par_master = new SqlParameterList();
                par_master.Add("@mCorpID", SqlDbType.Int, mCorpID);//用户ID
                ArrayList datalist = DBHelper.Select(sql, par_master.Get_List(), connstr);
                return datalist;
            }
            catch (Exception E)
            {
                return null;
            }
        }

        /// <summary>
        /// 门店地图
        /// </summary>
        public static ArrayList GetMapByStock(string shop_appid)
        {
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            try
            {
                string sql = @"exec pr_Vis_GetMapByStock @mCorpID";
                SqlParameterList par_master = new SqlParameterList();
                par_master.Add("@mCorpID", SqlDbType.Int, mCorpID);//用户ID
                ArrayList datalist = DBHelper.Select(sql, par_master.Get_List(), connstr);
                return datalist;
            }
            catch (Exception E)
            {
                return null;
            }
        }
    }
}